Programming Questions for Practice
Write a program to reverse the given array.
#include <stdio.h>
#include <stdlib.h>
/* Write a program to reverse the given array. */
void displayArray(int array[], int length) {
printf("Array is\n");
for(int i = 0; i < length; i++)
printf("%d ", array[i]);
}
int main()
{
// Creating an array of given length
int length;
printf("Enter the length of the array: ");
scanf("%d", &length);
int *array = (int *)calloc(length, sizeof(int));
printf("Enter any %d numbers: ", length);
for(int i = 0; i < length; i++)
scanf("%d", &array[i]);
displayArray(array, length);
// Logic of reverse the array
int first = 0, last = length-1;
while(first < last) {
int temp = array[first];
array[first] = array[last];
array[last] = temp;
first++;
last--;
}
// Display the result
printf("\nAfter reverse\n");
displayArray(array, length);
return 0;
}
Code not available...
Code not available...